import Vue from "vue"
import Vuex from "vuex"
import createPersistedState from "vuex-persistedstate"
Vue.use(Vuex)

const store = new Vuex.Store({
    // 定义状态
    state: {
        //审核数据
        callback: {},
        visitedTags: [],
        market_id: undefined,
        ptype: undefined,
        productId: undefined,
        showPriceTable: false,
        productDetail: {},
        autoOn: true,
        // disabled: false

        breadcrumbMenu: {}, //面包屑菜单
        admin_role: false
    },
    getters: {
        // getDisabled: state => state.disabled,
        getMarketId: state => state.market_id,
        getPtype: state => state.ptype,
        getPriceTableStatus: state => state.showPriceTable,
        getProductId: state => state.productId,
        getProductDetail: state => state.productDetail,
        getAutoOn: state => state.autoOn,
    },
    mutations: {
        setAutoOn(state, autoOn) {
            state.autoOn = autoOn
        },
        // setDisabled(state, disabled) {
        //     state.disabled = disabled
        // },
        setProductDetail(state, productDetail) {
            state.productDetail = productDetail
        },
        setProductId(state, productId) {
            state.productId = productId
        },
        setPriceTableStatus(state, showPriceTable) {
            state.showPriceTable = showPriceTable
        },
        setMarketId(state, market_id) {
            state.market_id = market_id
        },
        setPtype(state, ptype) {
            state.ptype = ptype + ''
        },
        setCallback(state, data) {
            try {
                state.callback[data.key] = data.func
            } catch (e) {
                console.log(e)
            }
        },
        closeOtherTags(state, path) {
            // let item = undefined
            let key = state.visitedTags.length - 1
            while (key >= 0) {
                let item = state.visitedTags[key]
                if (item.path !== path) {
                    state.visitedTags.splice(key, 1)
                }
                key--
            }
        },
        closeTags(state, path) {
            for (const [key, item] of state.visitedTags.entries()) {
                if (item.path === path) {
                    state.visitedTags.splice(key, 1)
                    break
                }
            }
        },
        addTags(state, payload) {
            if (!payload.name) return
            let flag = state.visitedTags.some((item) => item.path === payload.path)
            if (!flag) {
                state.visitedTags.push(payload)
            }
        },
        updateTags(state, payload) {
            for (const [key, item] of state.visitedTags.entries()) {
                if (item.path === payload.path) {
                    state.visitedTags[key].name = payload.name
                    break
                }
            }
        },

        setBreadcrumbMenu(state, menu) {
            if (!menu.twoMenu) {
                state.breadcrumbMenu.thrMenu = menu
            } else {
                state.breadcrumbMenu = menu
            }
        },
        removeBreadcrumbMenu(state) {
            state.breadcrumbMenu = {}
        },

        setAdminRole(state, admin_role) {
            state.admin_role = admin_role
        }
    },
    actions: {},
    plugins: [
        //持久化
        createPersistedState({
            storage: window.sessionStorage,
            reducer(val) { //指定持久化数据
                return {
                    breadcrumbMenu: val.breadcrumbMenu,
                    admin_role: val.admin_role
                }
            }
        })
    ],
})
export default store